var clsValidform = (function () {
    var Valid_bool = "";
    var conf = {
        /**
         * 字符串是否为空
         */
        isNotEmpty: function (str, $spanInput) {
            if (str == null || str == "") {
                Valid_bool += '0';
                if ($spanInput != null) $spanInput.show();
            } else {
                Valid_bool += '1';
                if ($spanInput != null) $spanInput.hide();
            }
            return this;
        },
        /**
         * 判读字符串的长度不能大于指定长度
         */
        isStrLength: function (str, len, $spanInput) {
            if (str.length > len) {
                Valid_bool += '0';
                if ($spanInput != null) $spanInput.html('<img src="/img/close.gif">输入字符过长，应小于 ' + len + '</img>');
            } else {
                Valid_bool += '1';
                if ($spanInput != null) $spanInput.empty();
            }
            return this;
        },
        /**
        * 匹配Email地址
        */
        isEmail: function (str) {
            if (str == null || str == "") Valid_bool = false;
            var result = str.match(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/);
            if (result == null) Valid_bool = false;
            Valid_bool = true;
            return this;
        },
        /**
        * 匹配phone
        */
        isPhone: function (str) {
            if (str == null || str == "") return false;
            var result = str.match(/^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/);
            if (result == null) return false;
            return true;
        },

        /**
         * 匹配mobile
         */
        isMobile: function (str) {
            if (str == null || str == "") return false;
            var result = str.match(/^((\(\d{2,3}\))|(\d{3}\-))?((13\d{9})|(15\d{9})|(18\d{9}))$/);
            if (result == null) return false;
            return true;
        },

        /**
         * 联系电话(手机/电话皆可)验证   
         */
        isTel: function (text, $spanInput) {
            if (this.isMobile(text) || this.isPhone(text)) {
                Valid_bool += '1';
                if ($spanInput != null) $spanInput.empty();
            } else {
                Valid_bool += '0';
                if ($spanInput != null) $spanInput.html('<img src="/img/close.gif">电话号码不符合要求！</img>');
            }
            return this;
        },
        /**
         * 判读两个字符串是否相等
         */
        isStrEqual: function (str, str1, $spanInput) {
            if (str == str1) {
                Valid_bool += '1';
                if ($spanInput != null) $spanInput.empty();
            } else {
                Valid_bool += '0';
                if ($spanInput != null) $spanInput.html('<img src="/img/close.gif">两次密码输入不一致！</img>');
            }
            return this
        },
        /**
        * 匹配integer
        */
        isInteger: function (str) {
            if (str == null || str == "") return false;
            var result = str.match(/^[-\+]?\d+$/);
            if (result == null) return false;
            return true;
        },
        /**
         * 匹配double或float
         */
        isDouble: function (str) {
            if (str == null || str == "") return false;
            var result = str.match(/^[-\+]?\d+(\.\d+)?$/);
            if (result == null) return false;
            return true;
        },


        /**
         * 匹配邮政编码
         */
        isZipCode: function (str) {
            if (str == null || str == "") return false;
            var result = str.match(/^[0-9]{6}$/);
            if (result == null) return false;
            return true;
        },

        /**
         * 匹配URL
         */
        isUrl: function (str) {
            if (str == null || str == "") return false;
            var result = str.match(/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\’:+!]*([^<>\"])*$/);
            if (result == null) return false;
            return true;
        },

        /**
         * 匹配密码，以字母开头，长度在6-12之间，只能包含字符、数字和下划线。
         */
        isPwd: function (str) {
            if (str == null || str == "") return false;
            var result = str.match(/^[a-zA-Z]\\w{6,12}$/);
            if (result == null) return false;
            return true;
        },

        /**
         * 判断是否为合法字符(a-zA-Z0-9-_)
         */
        isRightfulString: function (str) {
            if (str == null || str == "") return false;
            var result = str.match(/^[A-Za-z0-9_-]+$/);
            if (result == null) return false;
            return true;
        },

        /**
         * 匹配english
         */
        isEnglish: function (str) {
            if (str == null || str == "") return false;
            var result = str.match(/^[A-Za-z]+$/);
            if (result == null) return false;
            return true;
        },

        /**
         * 匹配身份证号码
         */
        isIdCardNo: function (num) {
            //　 if (isNaN(num)) {alert("输入的不是数字！"); return false;} 
            var len = num.length, re;
            if (len == 15)
                re = new RegExp(/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{2})(\w)$/);
            else if (len == 18)
                re = new RegExp(/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\w)$/);
            else { alert("输入的数字位数不对。"); return false; }
            var a = num.match(re);
            if (a != null) {
                if (len == 15) {
                    var D = new Date("19" + a[3] + "/" + a[4] + "/" + a[5]);
                    var B = D.getYear() == a[3] && (D.getMonth() + 1) == a[4] && D.getDate() == a[5];
                }
                else {
                    var D = new Date(a[3] + "/" + a[4] + "/" + a[5]);
                    var B = D.getFullYear() == a[3] && (D.getMonth() + 1) == a[4] && D.getDate() == a[5];
                }
                if (!B) { alert("输入的身份证号 " + a[0] + " 里出生日期不对。"); return false; }
            }
            if (!re.test(num)) { alert("身份证最后一位只能是数字和字母。"); return false; }

            return true;
        },

        /**
         * 匹配汉字
         */
        isChinese: function (str) {
            if (str == null || str == "") return false;
            var result = str.match(/^[\u4e00-\u9fa5]+$/);
            if (result == null) return false;
            return true;
        },

        /**
         * 匹配中文(包括汉字和字符)
         */
        isChineseChar: function (str) {
            if (str == null || str == "") return false;
            var result = str.match(/^[\u0391-\uFFE5]+$/);
            if (result == null) return false;
            return true;
        },

        /**
         * 字符验证，只能包含中文、英文、数字、下划线等字符。
         */
        stringCheck: function (str) {
            if (str == null || str == "") return false;
            var result = str.match(/^[a-zA-Z0-9\u4e00-\u9fa5-_]+$/);
            if (result == null) return false;
            return true;
        },

        /**
         * 过滤中英文特殊字符，除英文"-_"字符外
         */
        stringFilter: function (str) {
            var pattern = new RegExp("[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]");
            var rs = "";
            for (var i = 0; i < str.length; i++) {
                rs = rs + str.substr(i, 1).replace(pattern, '');
            }
            return rs;
        },

        /**
         * 判断是否包含中英文特殊字符，除英文"-_"字符外
         */
        isContainsSpecialChar: function (str) {
            if (str == null || str == "") return false;
            var reg = RegExp(/[(\ )(\`)(\~)(\!)(\@)(\#)(\$)(\%)(\^)(\&)(\*)(\()(\))(\+)(\=)(\|)(\{)(\})(\')(\:)(\;)(\')(',)(\[)(\])(\.)(\<)(\>)(\/)(\?)(\~)(\！)(\@)(\#)(\￥)(\%)(\…)(\&)(\*)(\（)(\）)(\—)(\+)(\|)(\{)(\})(\【)(\】)(\‘)(\；)(\：)(\”)(\“)(\’)(\。)(\，)(\、)(\？)]+/);
            return reg.test(str);
        },
        /**
         * 返回验证结果
         */
        getValidform: function () {
            return Valid_bool;
        },
        /**
         * 清空验证码
         */
        setValidformEmpty: function () {
            Valid_bool = '';
            return this;
        }
    }
    return conf;
}())
$(function () {
	var window_height = $(window).height();
	var window_width = $(window).width();
	$('body').css('height',window_height+'px');
	$("body .body .content").css('top',(window_height-$('body .body .content').height())/2+'px');
	$("body .body .content").css('left',(window_width-$('body .body .content').width())/2+'px');

	$("body .body .content .user input").focus(function(){
		$("body .body .content .user i").css('color','#135FF3');
	});
	$("body .body .content .user input").blur(function(){
		$("body .body .content .user i").css('color','');
	});
	$("body .body .content .pass input").focus(function(){
		$("body .body .content .pass i").toggle();
		$("body .body .content .pass i").css('color','#135FF3');
	});
	$("body .body .content .pass input").blur(function(){
		$("body .body .content .pass i").toggle();
		$("body .body .content .pass i").css('color','');
	});
	
	$("body .body .content .button .button").hover(function(){
		$(this).css("background","#a2d57b");
	},function(){
		$(this).css("background","");
	});

	$("body .body .content .register").hover(function(){
		$("body .body .content .register img:nth-child(1)").hide();
		$("body .body .content .register img:nth-child(2)").show();
	},function(){
		$("body .body .content .register img:nth-child(1)").show();
		$("body .body .content .register img:nth-child(2)").hide();
	});
});
// 注册
$(function(){
	$("body .body .content .register").click(function(){
		$("body .body .RegisterS").show();
	});
	$("body .body .RegisterS .delete i").click(function(){
		$("body .body .RegisterS").hide();
    });
    $(':input[type="submit"]').bind('click', function () {
        validbool = clsValidform.setValidformEmpty()
            .isNotEmpty($("#UserName").val(), $('#alert_user'))
            .isNotEmpty($("#PasswordHash").val(), $('#alert_password'))
            .getValidform();
        if (validbool.indexOf('0') < 0) {
            return true;
        }
        return false;
    })
    $('#RegisterButton').click(function () {
        var validbool = clsValidToForm.setValidformEmpty()
            .isNotEmpty($("#Reg_UserName").val(), $('#Er_UserName'))
            .isNotEmpty($("#Reg_PasswordHash").val(), $('#Er_PasswordHash'))
            .isNotEmpty($("#Reg_ConfirmThePasswordHash").val(), $('#Er_ConfirmThePasswordHash'))
            .isStrEqual($("#Reg_PasswordHash").val(), $("#Reg_ConfirmThePasswordHash").val(), $('#Er_PasswordHash'))
            .isNotEmpty($("#Reg_RealName").val(), $('#Er_RealName'))
            .isNotEmpty($("#Reg_WorkUnit").val(), $('#Er_WorkUnit'))
            .isEmail($("#Reg_MailBox").val(), $('#Er_MailBox'))
            .getValidform();
        alert(validbool);
        if (validbool.indexOf('0') < 0) {
            $.ajax({
                type: "POST",
                url: "/reguser",
                data: {
                    UserName: $("#Reg_UserName").val(),
                    PasswordHash: $("#Reg_PasswordHash").val(),
                    RealName: $("#Reg_RealName").val(),
                    WorkUnit: $("#Reg_WorkUnit").val(),
                    IncumbentPost: $("#Reg_IncumbentPost").val(),
                    PresentAddress: $("#Reg_PresentAddress").val(),
                    ZipCodeNumber: $("#Reg_ZipCodeNumber").val(),
                    SeatNumber: $("#Reg_SeatNumber").val(),
                    PhoneNumber: $("#Reg_PhoneNumber").val(),
                    MailBox: $("#Reg_MailBox").val(),
                    QQNumber: $("#Reg_QQNumber").val()
                },
                dataType: "json",
                success: function (data) {
                    console.log(data);
                    $("#res_info").html(data.reMessage);
                },
                error: function (data) {
                    console.log(data);
                    alert("error-------"+data);
                }
            });
        }
    });
});